package javabean;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

@SuppressWarnings("unused")
public class Userbean {
    private String username;
    private String surname;
    private String name;
    private String email;
    private String phone;
    private String password;
    private String confirmpassword;
    private String image;
    private String way;
    private String dgut_username;
    public Userbean() {
    }

    public Userbean(String username, String password, String email, String phone, String confirmpassword)
    {
        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.confirmpassword = confirmpassword;
    }

    public Userbean(String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    public Userbean(String username, String surname, String name)
    {
        this.username = username;
        this.surname = surname;
        this.name = name;
    }

    public Userbean(String username, String surname, String name, String email, String phone, String confirmpassword)
    {
        this.username = username;
        this.surname = surname;
        this.name = name;
        this.email = email;
        this.phone = phone;
        this.confirmpassword = confirmpassword;
    }

    public  void creatTable()
    {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `dgutmessage` (\n" +
                    "      `username` char(20) NOT NULL UNIQUE,\n" +
                    "      `surname` varchar(10),\n" +
                    "      `name` varchar(10) ,\n" +
                    "      `email` varchar(30),\n" +
                    "      `phone` varchar(15),\n" +
                    "      `image` varchar(100) NOT NULL DEFAULT 'dgut.jpg',\n" +
                    "       PRIMARY KEY (`username`)\n" +
                    "    );");
            stmt.executeUpdate("CREATE TABLE IF NOT EXISTS `usersmessage` (\n" +
                    "      `username` char(30) NOT NULL UNIQUE,\n" +
                    "      `password` varchar(30) NOT NULL,\n" +
                    "      `surname` varchar(10),\n" +
                    "      `name` varchar(10),\n" +
                    "      `image` varchar(100) NOT NULL DEFAULT 'dgut.jpg',\n" +
                    "      `email` varchar(30) NOT NULL,\n " +
                    "      `phone` varchar(15) NOT NULL,\n" +
                    "      `dgut_username` char(20),\n" +
                    "       PRIMARY KEY (`username`),\n" +
                    "       FOREIGN KEY (`dgut_username`) REFERENCES `dgutmessage`(`username`)\n" +
                    "    );");
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public boolean localexist() throws IOException {

        boolean flags = false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("select * from `usersmessage` where `username`='"+username+"';");
            flags = rset.next();
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return flags;
    }
    public boolean dgutexist() throws IOException {

        boolean flags = false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("select * from `dgutmessage` where `username`='"+username+"';");
            flags = rset.next();
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return flags;
    }
    public boolean localsave() throws IOException {

        try{
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("insert into `usersmessage` (`username`,`password`,`email`,`phone`) values ('"+username+"','"+password+"','"+email+"','"+phone+"');");
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return  true;
    }
    public boolean dgutsave() throws IOException {

        try{
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("insert into `dgutmessage` (`username`,`surname`,`name`) values ('"+username+"','"+surname+"','"+name+"');");
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return  true;
    }
    public boolean login() throws IOException {
        boolean flags = false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("select * from `usersmessage` where `username`='"+username+"' AND `password`='"+password+"';");
            flags = rset.next();
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return flags;
    }
    public void bind() throws IOException {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("update `usersmessage` set `dgut_username`='"+dgut_username+"'where `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void changelocal() throws IOException {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("update `usersmessage` set `surname`='"+surname+"',`name`='"+name+"',`email`='"+email+"',`phone`='"+phone+"' where `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void changedgut() throws IOException {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("update `dgutmessage` set `surname`='"+surname+"',`name`='"+name+"',`email`='"+email+"',`phone`='"+phone+"' where `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void localshow() throws IOException
    {
        boolean flags = false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("select * from `usersmessage` where `username`='"+username+"';");
            flags=rset.next();
            if(flags){
                surname = rset.getString("surname");
                name = rset.getString("name");
                email = rset.getString("email");
                phone = rset.getString("phone");
                image = rset.getString("image");
                dgut_username = rset.getString("dgut_username");
            }
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public void dgutshow() throws IOException
    {
        boolean flags = false;
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            ResultSet rset = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset=stmt.executeQuery("select * from `dgutmessage` where `username`='"+username+"';");
            flags=rset.next();
            if(flags){
                surname = rset.getString("surname");
                name = rset.getString("name");
                email = rset.getString("email");
                phone = rset.getString("phone");
                image = rset.getString("image");
            }
            rset.close();
            stmt.close();
            conn.close();
            context.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public void changelocalimage()throws IOException
    {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("update `usersmessage` set `image`='"+image+"' where `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void changedgutimage()throws IOException
    {
        try {
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("jdbc/DSTest");
            Connection conn = null;
            Statement stmt = null;
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate("update `dgutmessage` set `image`='"+image+"' where `username`='"+username+"';");
            stmt.close();
            conn.close();
            context.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getConfirmpassword() {
        return confirmpassword;
    }

    public void setConfirmpassword(String confirmpassword) {
        this.confirmpassword = confirmpassword;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getWay() {
        return way;
    }

    public void setWay(String way) {
        this.way = way;
    }

    public String getDgut_username() {
        return dgut_username;
    }

    public void setDgut_username(String dgut_username) {
        this.dgut_username = dgut_username;
    }
}
